<?php
namespace app\admin\controller;
use app\admin\model\Merchant as MerchantModel;
use think\Db;
header("content-type:text/html; charset=utf-8");
/**
* 商户资料管理
*/
class Merchant extends Common
{	
	// 列表
	public function index(){
		// $Merchant=db('merchant')->select();
		$Merchant=Db::table('app_merchant')->field('a.id,a.name,a.mobile,a.contacts,a.address,a.thumb,a.status,b.register_time,b.salesname,rwm_id')->alias('a')->join('app_home b ','b.id= a.home_id')->order('b.register_time desc')->select();//关联查询
		$this->assign('Merchant',$Merchant);
		return view();
	}
	// 注册信息列表
	public function homes(){
		$home=db('home')->select();
		$this->assign('home',$home);
		return view();
	}
	// 商户详细信息列表
	public function detials(){
		$id=input('id');
		$Merchant=db('merchant')->where(array('id'=>$id))->find();
		$this->assign('Merchant',$Merchant);
		return view();
	}
	// 添加
	public function add(){
		if (request()->isPost()) {
            $data=input('post.');//接受数据
            // $sh=db('merchant')->field('id,name')->select();
            // foreach ($sh as $k=> $v) {
            // 	$data['id']=$v['id'];//给$data加字段id
            // }
            $Merchant=new MerchantModel();
            if ($Merchant->save($data)) {
                $this->success('添加成功！',url('index'));
            }else{
                $this->error('添加失败！');
            }
            return;
        }
	
		$sales=db('sales')->select();
		// 查询所有前台注册用户
		$home=db('home')->field('id,moblie')->select();
		$this->assign(
			array(
				'home'=>$home,
				'sales'=>$sales
				)
			
			);
		return view();
	}
	// 二维码上传
	public function code(){
		$id=input('id');//当前商户id
		if (request()->isPost()) {
            $data=input('post.');//接受数据
            $data['id']=$id;
            $Merchant=new MerchantModel();
            if ($Merchant->save($data,['id'=>$data['id']])) {
                $this->success('添加二维码成功！',url('index'));
            }else{
                $this->error('添加二维码失败！');
            }
            return;
        }
		$sh=db('merchant')->field('id,name')->where('id','=',$id)->select();
		$this->assign('sh',$sh);
		return view();
	}
	// 编辑
	public function edit(){
		if (request()->isPost()) {
			$data=input('post.');
			// var_dump($data);die;
			$Merchant=new MerchantModel();
			if ($Merchant->update($data)) {
				$this->success('修改成功！','index');
			}else{
				$this->error('修改失败！');
			}


		}
		
		
	}
//************************************

	public function rwm_pz(){
		$mid = $_GET['mid'];
		$this->assign('mid',$mid);
		$rwm = db('rwm')->where('types = 1')->select();
		$this->assign('rwm',$rwm);
		return view();
		
		
	}

	public function rwm_pz_ps(){
		$mid = $_POST['mid'];
		$rwmid = $_POST['rwmid'];
		$data = array(
            'mid' => $mid,
            'types' => 0
			);
		$rwm = db('rwm')->where('id = '.$rwmid.'')->update($data);
		if($rwm == 1){
			$dadf = array(
               'rwm_id' => $rwmid
				);
			$user = db('merchant')->where('id = '.$mid.'')->update($dadf);
			if($user == 1){
                  $this->success('配置二维码成功',url('Merchant/index'));
			}else{
				$this->error('配置二维码失败',url('Merchant/index'));
			}
           
		}else{
			$this->error('分配二维码失败',url('Merchant/index'));
		}

	}


	//生成二维码
	public function rwm_list(){
		$rwm = db('rwm')->where('types = 1')->select();
		$this->assign('rwm',$rwm);
		return view();
	}

	public function rwm_add(){
		// $sales=db('sales')->select();
  //      $this->assign('sales',$sales);
       

		return view();
	}

	public function rwm_add_ps(){
	
       $number = (int)$_POST['number'];
	   $sales_id = $_POST['sales_id'];
		$data = array(
            'salesname' => $sales_id,
            'register_time' => strtotime(date("Y-m-d H:i:s"))
			);
		for($i=0; $i<$number ; $i++){
       $rwm = db('rwm')->insert($data);
		}
		if($rwm != 0){
              $this->success('批量生成二维码成功',url('Merchant/rwm_list'));
		}else{
		      $this->error('批量生成二维码失败',url('Merchant/rwm_list'));
		}

	}

//下载二维码******************
   public function xiazai(){

   $statr = $_POST['fiel'];
   $statr = implode(',', $statr);
   $user = db('rwm')->where("mid in(".$statr.")")->field('id,salesname,url,mid')->select();
    if(!empty($user)){
		
   foreach ($user as $k=>$v){
$user[$k]['image_src'] = "http://pan.baidu.com/share/qrcode?w=350&h=350&url=http://hb.nmejiefu.com/hb.php?mid=".$user[$k]['id'].""; // 要下载的文件
$name = date("Y-m-d H:i:s");
 $user[$k]['image_name'] =  $name.'---bianhao---'.$user[$k]['id'].'.png';
   }
//配置下载文件信息----------文件名称----------------------文件路径--------------↑

 $dfile =  tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
 vendor("zipfile.zipfile");
 $zip = new \zipfile();
$filename = 'yipeizhi.zip'; //下载的默认文件名zip
foreach($user as $k=>$v){
    $zip->add_file(file_get_contents($v['image_src']),  $v['image_name']);
    // 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称, 可包含路径
    // 或是想打包整个目录 用 $zip->add_path($image_path);
}
//----------------------
$zip->output($dfile);

// 下载文件
ob_clean();
header('Pragma: public');
header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control:no-store, no-cache, must-revalidate');
header('Cache-Control:pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding:binary');
header('Content-Encoding:none');
header('Content-type:multipart/form-data;charset=utf-8');
header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
header('Content-length:'. filesize($dfile));
$fp = fopen($dfile, 'r');
while(connection_status() == 0 && $buf = @fread($fp, 8192)){
    echo $buf;
}
fclose($fp);
@unlink($dfile);
@flush();
@ob_flush();
exit();

 }else{
 	$this->error('您商户未配置二维码',url('Merchant/index'));
 }
   }

//下载没配置二维码******************
   public function weipeizhi(){

   $statr = $_POST['fiel'];
   $statr = implode(',', $statr);
   $user = db('rwm')->where("id in(".$statr.")")->field('id,salesname,url,mid')->select();
    if(!empty($user)){
		
   foreach ($user as $k=>$v){
$user[$k]['image_src'] = "http://pan.baidu.com/share/qrcode?w=350&h=350&url=http://hb.nmejiefu.com/hb.php?mid=".$user[$k]['id'].""; // 要下载的文件
$name = date("Y-m-d H:i:s");
 $user[$k]['image_name'] =  $name.'---bianhao---'.$user[$k]['id'].'.png';
   }
//配置下载文件信息----------文件名称----------------------文件路径--------------↑

 $dfile =  tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
 vendor("zipfile.zipfile");
 $zip = new \zipfile();
$filename = 'weipeizhi.zip'; //下载的默认文件名zip
foreach($user as $k=>$v){
    $zip->add_file(file_get_contents($v['image_src']),  $v['image_name']);
    // 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称, 可包含路径
    // 或是想打包整个目录 用 $zip->add_path($image_path);
}
//----------------------
$zip->output($dfile);

// 下载文件
ob_clean();
header('Pragma: public');
header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control:no-store, no-cache, must-revalidate');
header('Cache-Control:pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding:binary');
header('Content-Encoding:none');
header('Content-type:multipart/form-data;charset=utf-8');
header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
header('Content-length:'. filesize($dfile));
$fp = fopen($dfile, 'r');
while(connection_status() == 0 && $buf = @fread($fp, 8192)){
    echo $buf;
}
fclose($fp);
@unlink($dfile);
@flush();
@ob_flush();
exit();

 }else{
 	$this->error('您商户未配置二维码',url('Merchant/index'));
 }
   }


}